1 <?php
2 require("../includes/config.php");
3 session_start();
4 $currentDate = date('Y-m-d');
5 $order_id = $_POST['order_id'];
6 if($_SERVER["REQUEST_METHOD"] == "POST") {
7 if(!isset($_POST['distributor'])) {
8 $_SESSION['error'] = "* Please choose distributor";
9 header("Location:generate_invoice.php?id=$order_id");
10 }
11 else {
12 $comment = "";
13 $dist_id = $_POST['distributor'];
14 if(isset($_POST['txtComment'])) {
15 if(!empty($_POST['txtComment'])) {
16 $comment = $_POST['txtComment'];
17 }
18 }
19 $query_selectOrder = "SELECT * FROM orders WHERE order_id='$order_id'";
20 $result_selectOrder = mysqli_query($con,$query_selectOrder);
21 $row_selectOrder = mysqli_fetch_array($result_selectOrder);
22 $retailer_id = $row_selectOrder['retailer_id'];
23 $total_amount = $row_selectOrder['total_amount'];
24 $query_selectInvoiceId = "SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='scm_new' AND TABLE_NAME='invoice'";
25 $result_selectInvoiceId = mysqli_query($con,$query_selectInvoiceId);
26 $row_selectInvoiceId = mysqli_fetch_array($result_selectInvoiceId);
27 $invoice_id = $row_selectInvoiceId['AUTO_INCREMENT'];
28 $queryInsertInvoice = "INSERT INTO invoice(order_id,retailer_id,dist_id,date,total_amount,comments) VALUES('$order_id','$retailer_id','$dist_id','$currentDate','$total_amount','$comment')";
29 if(mysqli_query($con,$queryInsertInvoice)) {
30 $query_selectOrderItems = "SELECT * FROM order_items WHERE order_id='$order_id'";
31 $result_selectOrderItems = mysqli_query($con,$query_selectOrderItems);
32 while($row_selectOrderItems = mysqli_fetch_array($result_selectOrderItems)) {
33 $product_id = $row_selectOrderItems['pro_id'];
34 $quantity = $row_selectOrderItems['quantity'];
35 $queryInsertInvoiceItems = "INSERT INTO invoice_items(invoice_id,product_id,quantity) VALUES('$invoice_id','$product_id','$quantity')";
36 $result = mysqli_query($con,$queryInsertInvoiceItems);
37 }
38 if($result == 1) {
39 $queryUpdateStatus = "UPDATE orders SET status=1 WHERE order_id='$order_id'";
40 if(mysqli_query($con,$queryUpdateStatus)) {
41 echo "<script> alert(\"Invoice Generated Successfully\"); </script>";
42 header("Refresh:0;url=view_invoice_items.php?id=$invoice_id");
43 }
44 else {
45 echo "Can not update the status of order.";
46 }
47 }
48 }
49 else {
50 echo "There was some issue";
51 }
52 }
53 }
54 ?>